//
//  UserRegWaitViewCtrl.m
//  CurvesFit
//
//  Created by NCIT Developer on 10/24/16.
//  Copyright © 2016 Nci Technology Inc. All rights reserved.
//

#import "UserRegWaitViewCtrl.h"
#import "AppDelegate.h"
#import "ImgSwipeViewController.h"
#import "UIView+Extension.h"
static int _nImgIdx = 0;
static int _nImgCount = 4;
static BOOL _bDesImgIdx = NO;
static BOOL _bEnterCreateUI = NO;
static NSTimer* _imgChgTimer = nil;

@interface UserRegWaitViewCtrl ()

@end

@implementation UserRegWaitViewCtrl {
  UILabel* _lblCreateTips;
  
  BOOL _biPad;
  BOOL _biP6p;
  BOOL _biP5s;
  BOOL _bWider;
  BOOL _bShowStatusBar;
  
  UILabel* _lblFitFips;
  UILabel* _lblTrackerTips;
  
  UIButton* _btnRegUser;
  UIButton* _btnLoginUser;
  UIPageControl* _pageC;
    UIImageView *_imgView;
  ImgSwipeViewController* _imgSwpViewCtrl;
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [_btnLoginUser setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_btnRegUser setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//    _imgView = [[UIImageView alloc] initWithFrame:self.view.frame];
//    _imgView.contentMode = UIViewContentModeScaleAspectFill;
//    _imgView.image = [UIImage imageNamed:@"ic-launch1"];
//    [self.view insertSubview:_imgView atIndex:0];
}

- (void)viewDidLoad {
  [super viewDidLoad];
    // Do any additional setup after loading the view.
  AppDelegate* pApp = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  self.navigationController.delegate = self;
  
  CGRect frame = [[UIScreen mainScreen] bounds];
  //UICoordinateSpace space = [[UIScreen.mainScreen.coordinateSpace];
  float fScale = [UIScreen mainScreen].scale;
  CGFloat W = frame.size.width;
  CGFloat H = frame.size.height;
  
  _biPad = H > 1000;
  _biP6p = fScale >= 3.0;
  _biP5s = H >= 568 && W == 320;
  _bWider = W > 320;
  
  CGRect rt = self.view.window.bounds;
  rt.origin.y -= 44;
  self.view.frame = rt;
  UIImage* imgback = [pApp getImageScale:@"ic-launch1.png"  scale:(_biPad? 1.30:_biP6p? 1.0: 0.64)];
  [self.view setBackgroundColor:[UIColor colorWithPatternImage:imgback]];
    
  //CGFloat xPos = (W-300)/ 2.0;//_biPadDev? :20;
  CGFloat yPos = H * 0.75; //_biPad? H / 1.60: H / (_bWider?0.50: 0.42) + (_biP5s? 45:30);
  //_btnRegUser = [[UIButton alloc] initWithFrame:CGRectMake(xPos, yPos, 260, 30)];
  if(!_bEnterCreateUI) {
    _lblFitFips = [[UILabel alloc] initWithFrame:CGRectMake(15, H * 0.58, W-30, H / 13.6)];
    _lblFitFips.font = Bold(H / 14);

    _lblFitFips.textColor = [UIColor whiteColor];
    _lblFitFips.textAlignment = NSTextAlignmentLeft;
    _lblFitFips.text = @"F.I.T.";
    [self.view addSubview:_lblFitFips];
    
    
    _lblTrackerTips = [[UILabel alloc] initWithFrame:CGRectMake(15, CGRectGetMaxY(_lblFitFips.frame) + 10, W - 30, H / 30)];
    _lblTrackerTips.font = Regular(H / 35);
    _lblTrackerTips.textColor = [UIColor whiteColor];
    _lblTrackerTips.textAlignment = NSTextAlignmentLeft;
    
    _lblTrackerTips.text = NSLocalizedString(@"Your personal fitness tracker", nil);
    [self.view addSubview:_lblTrackerTips];
  }
  else {
    _lblFitFips = nil;
    _lblTrackerTips = nil;
  }
  
  if(!_bEnterCreateUI) {
    _btnRegUser = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    _btnRegUser.frame = CGRectMake(15, H - 2 * (H / 16.7) - 30, W - 30, H / 16.7);
    [_btnRegUser setTitle:[NSLocalizedString(@"Create An Account", nil) uppercaseString]forState:UIControlStateNormal];
    _btnRegUser.backgroundColor = [UIColor colorWithRed:112.0/255 green:42.0/255 blue:135.0/255 alpha:1.0];
    _btnRegUser.layer.cornerRadius = 8;
    _btnRegUser.tintColor = [UIColor whiteColor];
    [_btnRegUser addTarget:self action:@selector(onRegCreate:) forControlEvents:UIControlEventTouchUpInside];
     _btnRegUser.titleLabel.font = Semibold(16);
    [self.view addSubview:_btnRegUser];
      _pageC.centerY = _btnRegUser.centerY - (H / 16.7) / 2 - 20;
    
    yPos += 50;
    _btnLoginUser = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    _btnLoginUser.frame = CGRectMake(15, H - (H / 16.7) - 20, W - 30, H / 16.7);
    //_btnRegUser.titleLabel.text = @"Register User";
    [_btnLoginUser setTitle:NSLocalizedString(@"LOGIN TO F.I.T", nil) forState:UIControlStateNormal];
    _btnLoginUser.backgroundColor = [UIColor colorWithRed:215.0/255 green:26.0/255 blue:117.0/255 alpha:1.0];//[UIColor redColor];
    _btnLoginUser.layer.cornerRadius = 8;
    _btnLoginUser.tintColor = [UIColor whiteColor];
    [_btnLoginUser addTarget:self action:@selector(onLoginUser:) forControlEvents:UIControlEventTouchUpInside];
      _btnLoginUser.titleLabel.font = Semibold(16);
    [self.view addSubview:_btnLoginUser];
  }
  
  _imgSwpViewCtrl = nil;
  if(!_bEnterCreateUI) {
    [self setupSwipeGesture];
    [self addPageControl:W hgt:H];
    _imgChgTimer = [NSTimer scheduledTimerWithTimeInterval:3.00 target:self selector:@selector(onImgCheTimer) userInfo:nil repeats:YES];
  }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void) navigationController:(UINavigationController*) navigationController willShowViewController:(nonnull UIViewController *)viewController animated:(BOOL)animated
{
  BOOL isShowHomePage = [viewController isKindOfClass:[self class]];
  [self.navigationController setNavigationBarHidden:isShowHomePage animated:YES];
}

- (BOOL) prefersStatusBarHidden {
  return YES;
}

- (void) setupSwipeGesture
{
  UISwipeGestureRecognizer * swipeGestureLeft = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeGesture:)];
  swipeGestureLeft.direction = UISwipeGestureRecognizerDirectionLeft;
  [self.view addGestureRecognizer:swipeGestureLeft];
  
  UISwipeGestureRecognizer * swipeGestureRight = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeGesture:)];
  swipeGestureRight.direction = UISwipeGestureRecognizerDirectionRight;
  [self.view addGestureRecognizer:swipeGestureRight];

}

- (void) handleSwipeGesture:(UIGestureRecognizer *)sender
{
  UISwipeGestureRecognizerDirection direction = [(UISwipeGestureRecognizer *)sender direction];
  
  BOOL bToLeft = direction == UISwipeGestureRecognizerDirectionLeft;
  BOOL bToRight = direction == UISwipeGestureRecognizerDirectionRight;
  
  CATransition* animation = [CATransition animation];
  [animation setDuration:0.5f];
  [animation setType:kCATransitionReveal];
  
  if(bToLeft)
    _bDesImgIdx = NO;
  else if(bToRight)
    _bDesImgIdx = YES;
  
  if((bToLeft || bToRight) && _imgSwpViewCtrl == nil) {
    [self changeViewCtrlBkgdImg];
    //[_imgChgTimer invalidate];
    //_nImgIdx++;
  }
}

- (void) onImgCheTimer
{
  int nCount = _nImgCount - 1;
  if(_nImgIdx <= 0)
    _bDesImgIdx = NO;
  else if(_nImgIdx >= nCount)
    _bDesImgIdx = YES;
  [self changeViewCtrlBkgdImg];
}

- (void) changeViewCtrlBkgdImg
{
  int nCount = _nImgCount - 1;
  if(_bDesImgIdx)
    _nImgIdx--;
  else
    _nImgIdx++;
  
  if(_nImgIdx < 0)
    _nImgIdx = 0;
  if(_nImgIdx >= nCount)
    _nImgIdx = nCount;
  
  AppDelegate* pApp = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  //[pApp enterPageWithRes:@"imgSwpViewCtrl"];
  NSString* sImgName = @"";
  //if(_nImgIdx <= 0)
  //sImgName = _biPad?@"ic-Launch@1024.png":@"ic-launch4.png";
  //else
  sImgName = [NSString stringWithFormat:@"ic-launch%i.png", _nImgIdx+1];
  float fRate = _biPad? 1.30:_biP6p? 1.0: 0.64;
  UIImage* imgback = [pApp getImageScale:sImgName scale:fRate];
  [self.view setBackgroundColor:[UIColor colorWithPatternImage:imgback]];
//    _imgView.image = [UIImage imageNamed:sImgName];
  CGRect frame = [[UIScreen mainScreen] bounds];
  CGFloat W = frame.size.width;
  if(_nImgIdx == 0) {
    CGRect rt1 = _lblFitFips.frame;
    CGRect rt2 = _lblTrackerTips.frame;
    rt1.origin.x = (W - 300) / 2 + 0;
    rt2.origin.x = (W- 300) / 2 + 0;
    
    _lblFitFips.text = @"F.I.T.";
    _lblTrackerTips.text = NSLocalizedString(@"Your personal fitness tracker", nil);
      _lblFitFips.textAlignment = NSTextAlignmentLeft;
      _lblTrackerTips.textAlignment = NSTextAlignmentLeft;
//    _lblFitFips.frame = rt1;
//    _lblTrackerTips.frame = rt2;
  }
  else if(_nImgIdx == 1) {
    CGRect rt1 = _lblFitFips.frame;
    CGRect rt2 = _lblTrackerTips.frame;
    rt1.origin.x = (W - 300) / 2 + 145;
    rt2.origin.x = (W - 300) / 2 + 68;
    
    _lblFitFips.text = @"Progress";
    _lblTrackerTips.text = NSLocalizedString(@"Record, store & track your workout", nil);
      _lblFitFips.textAlignment = NSTextAlignmentRight;
      _lblTrackerTips.textAlignment = NSTextAlignmentRight;
//    _lblFitFips.frame = rt1;
//    _lblTrackerTips.frame = rt2;

    
  }
  else if(_nImgIdx == 2) {
    CGRect rt1 = _lblFitFips.frame;
    CGRect rt2 = _lblTrackerTips.frame;
    rt1.origin.x = (W - 300) / 2 + 0;
    rt2.origin.x = (W - 300) / 2 + 0;
    
    _lblFitFips.text = @"Activity";
    _lblTrackerTips.text = NSLocalizedString(@"Real time data as you workout", nil);
      _lblFitFips.textAlignment = NSTextAlignmentLeft;
      _lblTrackerTips.textAlignment = NSTextAlignmentLeft;
//    _lblFitFips.frame = rt1;
//    _lblTrackerTips.frame = rt2;

  }
  else if(_nImgIdx == 3) {
    CGRect rt1 = _lblFitFips.frame;
    CGRect rt2 = _lblTrackerTips.frame;
    rt1.origin.x = (W - 300) / 2 + 112;
    rt2.origin.x = (W - 300) / 2 + 60;
    
    _lblFitFips.text = @"Connected";
    _lblTrackerTips.text = NSLocalizedString(@"Syncing data from a range of devices", nil);
      _lblFitFips.textAlignment = NSTextAlignmentRight;
      _lblTrackerTips.textAlignment = NSTextAlignmentRight;
//    _lblFitFips.frame = rt1;
//    _lblTrackerTips.frame = rt2;
  }
  
  NSInteger nIdx =  _nImgIdx;
  //if(nIdx < 0)
    //nIdx = 0;
  _pageC.currentPage = nIdx;
}

-(void)addPageControl:(CGFloat)W hgt:(CGFloat) H {
  _pageC = [UIPageControl new];
  CGSize size= [_pageC sizeForNumberOfPages:_nImgCount]; //根据页数返回 UIPageControl 合适的大小
  //size.width = size.width / 2.5;
  _pageC.frame = CGRectMake(/*(W-size.width) / 8*/-30, H * 0.76, size.width, size.height);
  //_pageC.center = CGPointMake(kWidthOfScreen / 2.0, kHeightOfScreen - 80.0);
  _pageC.numberOfPages = _nImgCount;
  _pageC.pageIndicatorTintColor = [UIColor whiteColor];
  _pageC.currentPageIndicatorTintColor = [UIColor brownColor];
  _pageC.userInteractionEnabled = NO; //设置是否允许用户交互；默认值为 YES，当为 YES 时，针对点击控件区域左（当前页索引减一，最小为0）右（当前页索引加一，最大为总数减一），可以编写 UIControlEventValueChanged 的事件处理方法
  _pageC.backgroundColor = [UIColor clearColor];
  [self.view addSubview:_pageC];
}

-(void) onRegCreate:(id)sender
{
  if(_imgChgTimer != nil) {
    [_imgChgTimer invalidate];
  }
  AppDelegate* pApp = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  [pApp showLogingInToolBar];
  [pApp enterPageWithRes:@"userRegViewCtrl"];
  /*if(_imgChgTimer != nil) {
    [_imgChgTimer invalidate];
  }
  
  if(_lblFitFips != nil)
    _lblFitFips.hidden = YES;
  if(_lblTrackerTips != nil)
    _lblTrackerTips.hidden = YES;
  
  _btnRegUser.hidden = YES;
  _btnLoginUser.hidden = YES;
  
  CGRect frame = [[UIScreen mainScreen] bounds];
  CGFloat W = frame.size.width;
  CGFloat H = frame.size.height;
  [self createRegUserInfoUi:W h:H];
  
  _bShowStatusBar = YES;
  self.navigationItem.hidesBackButton = NO;
  [self.navigationController setNavigationBarHidden:NO animated:YES];
  [self performSelector:@selector(setNeedsStatusBarAppearanceUpdate)];
  
  AppDelegate* pApp = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  [pApp showLogingInToolBar];
  
  UIBarButtonItem *btnBack = [[UIBarButtonItem alloc] initWithTitle:@"Back"
                                                              style:UIBarButtonItemStylePlain
                                                             target:self action:@selector(onRetnRegWaitUI:)];
  //[[self navigationItem] setBackBarButtonItem:back];
  //[self.navigationController.navigationItem setBackBarButtonItem:back];
  self.navigationItem.leftBarButtonItem = btnBack;
  _pageC.hidden = YES;*/
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (void) createRegUserInfoUi:(CGFloat) W h:(CGFloat)H
{
  if(_imgChgTimer != nil) {
    [_imgChgTimer invalidate];
  }
  AppDelegate* pApp = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  [pApp enterPageWithRes:@"userProfViewCtrl"];
}

/*- (void) onImgCheTimer
{
  int nCount = _nImgCount - 1;
  if(_nImgIdx <= 0)
    _bDesImgIdx = NO;
  else if(_nImgIdx >= nCount)
    _bDesImgIdx = YES;
  //else if(_nImgIdx > 0 && _bDesImgIdx)
  //_bDesImgIdx =  YES;
  [self changeViewCtrlBkgdImg:nCount];
}*/

- (void) onLoginUser:(id)sender
{
  if(_imgChgTimer != nil) {
    [_imgChgTimer invalidate];
  }
  AppDelegate* pApp = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  [pApp showLogingInToolBar];
  [pApp enterPageWithRes:@"loginViewCtrl"];
}

@end
